package com.diyunapp.xilinapp.login;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.diyunapp.xilinapp.R;
import com.diyunapp.xilinapp.main.MainActivity;
import com.diyunapp.xilinapp.selfdefine.StatusBarFragmentActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends StatusBarFragmentActivity {

    @Bind(R.id.go_back)
    ImageView goBack;
    @Bind(R.id.userlogin_et)
    EditText userloginEt;
    @Bind(R.id.user_password_et)
    EditText userPasswordEt;
    @Bind(R.id.sure_password_et)
    EditText surePasswordEt;
    @Bind(R.id.user_email_et)
    EditText userEmailEt;
    @Bind(R.id.login_tv)
    TextView loginTv;
    @Bind(R.id.user_tjm_et)
    EditText userTjmEt;

private String phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        View headView = findViewById(R.id.register_menubar_inc);
        super.MenuWidget(headView);
        phoneNum=getIntent().getExtras().getString("phoneNum");
    }

    @OnClick({R.id.go_back, R.id.login_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.go_back:
                finish();
//                nextAct(LoginActivity.class);
                break;

            case R.id.login_tv:
                if (userloginEt.getText().toString().trim() == "") {
                    toast("请输入用户名");
                } else if (userPasswordEt.getText().toString().trim() == "") {
                    toast("请输入密码");
                } else if (surePasswordEt.getText().toString().trim() == "") {
                    toast("请确认密码");
                } else if (userEmailEt.getText().toString().trim() == "") {
                    toast("请输入邮箱");
                }
//                else if(userPasswordEt.getText().toString().trim()!=surePasswordEt.getText().toString().trim()){
//                    toast("两次密码不一致");
//                }
                else {
                    //zhuce
                    regieter();
//                    toast("注册成功");
                }
        }
    }

    public void regieter() {
        String username = userloginEt.getText().toString().trim();
        String password = userPasswordEt.getText().toString().trim();
        String surepassword = surePasswordEt.getText().toString().trim();
        String email = userEmailEt.getText().toString().trim();
        String tjm = userTjmEt.getText().toString().trim();
        String url = MainActivity.url + "act=login&op=register";
        Log.i("zhangxiaoxidebug", "resultinfo=" + url);
        RequestParams params = new RequestParams();
        params.addBodyParameter("username", username);
        Log.i("zhangxiaoxidebug","++"+username);
        params.addBodyParameter("password", password);
        Log.i("zhangxiaoxidebug","++1"+password);
        params.addBodyParameter("password_confirm", surepassword);
        Log.i("zhangxiaoxidebug","++2"+surepassword);
        params.addBodyParameter("phone",phoneNum);
        Log.i("zhangxiaoxidebug","++3"+phoneNum);
        params.addBodyParameter("email",email);
        Log.i("zhangxiaoxidebug","++4"+email);
        params.addBodyParameter("client","android");
        params.addBodyParameter("from_member_tjcode",tjm);
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000);
        http.send(HttpRequest.HttpMethod.POST, url,params,
                new RequestCallBack<String>() {
                    @Override
                    public void onLoading(long total, long current, boolean isUploading) {

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
//                        bidFreshLv.onRefreshComplete();
                        String resultinfo = responseInfo.result;
                        Log.i("zhangxiaoxidebug", "resultinfo1=" + resultinfo);
                        try {
                            JSONObject resultObject = new JSONObject(resultinfo);
//                            int result = resultObject.getInt("code");
                            JSONObject datas = resultObject.optJSONObject("datas");
                            Log.i("zhangxiaoxidebug", "datas" + datas);
                            String error = datas.optString("error");
                            if (error != "") {
                                toast(error);
                            }
                            String username = datas.optString("username");
                            if (username != "") {
                                toast("注册成功");
                                setResult(1);
                                finish();
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
//                            toast("查询失败");
                            e.printStackTrace();

                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        toast("请求失败");
                    }
                });
    }

}
